package com.fionera.sample.activity

import android.annotation.SuppressLint
import android.os.Bundle
import android.support.annotation.IntDef
import android.support.v4.content.ContextCompat
import android.view.View
import android.widget.LinearLayout
import com.fionera.sample.R
import com.fionera.sample.widget.TitleBar

/**
 * @author fionera
 */
@SuppressLint("Registered")
open class TitleBarActivity : BaseActivity() {

    private lateinit var mLayoutMain: View
    private lateinit var titleBar: TitleBar
    private lateinit var mLayoutContent: LinearLayout

    @IntDef(View.VISIBLE, View.INVISIBLE, View.GONE)
    @Retention(AnnotationRetention.SOURCE)
    internal annotation class Visibility

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mLayoutMain = View.inflate(mContext, R.layout.title_bar_content, null)
        mLayoutContent = findViewById(R.id.title_bar_content)
        titleBar = findViewById(R.id.title_bar)
        titleBar.setLeftDrawable(R.drawable.ic_action_back)
        titleBar.setTitleBarLeftClick { finish() }
    }

    override fun <T : View> findViewById(id: Int): T {
        return mLayoutMain.findViewById(id) ?: return super.findViewById(id)
    }

    override fun setContentView(layoutResID: Int) {
        val mContent = layoutInflater.inflate(layoutResID, null)
        mLayoutContent.addView(mContent, LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT)
        super.setContentView(mLayoutMain)
    }

    fun setTitleBarText(title: String) {
        titleBar.setTitleBarText(title)
    }

    fun setTitleBarColor(color: Int) {
        titleBar.setTitleBarColor(ContextCompat.getColor(mContext, color))
    }

    fun setTitleBarLeft(drawable: Int) {
        titleBar.setLeftDrawable(drawable)
    }

    fun setTitleBarRight(drawable: Int) {
        titleBar.setRightDrawable(drawable)
    }

    fun setTitleBarLeft(leftText: String) {
        titleBar.setLeftText(leftText)
    }

    fun setTitleBarRight(rightText: String) {
        titleBar.setRightText(rightText)
    }

    fun setTitleBarRight(rightText: String, iconId: Int, iconAtLeft: Boolean) {
        titleBar.setRightTextAndIcon(rightText, iconId, iconAtLeft)
    }

    fun setTitleBarLeft(leftText: String, iconId: Int, iconAtLeft: Boolean) {
        titleBar.setLeftTextAndIcon(leftText, iconId, iconAtLeft)
    }

    fun setTitleBarLeftVisibility(@Visibility visibility: Int) {
        titleBar.setLeftVisibility(visibility)
    }

    fun setTitleBarRightVisibility(@Visibility visibility: Int) {
        titleBar.setRightVisibility(visibility)
    }

    fun setTitleBarVisibility(@Visibility visibility: Int) {
        titleBar.visibility = visibility
    }

    fun setTitleBarLeftClick(onLeftClickListener: View.OnClickListener) {
        titleBar.setTitleBarLeftClick(onLeftClickListener)
    }

    fun setTitleBarRightClick(onRightClickListener: View.OnClickListener) {
        titleBar.setTitleBarRightClick(onRightClickListener)
    }

    fun setTitleBarTitleClick(onTitleClickListener: View.OnClickListener) {
        titleBar.setTitleBarTitleClick(onTitleClickListener)
    }

    fun setTitleBarRightFirstClick(onRightClickListener: View.OnClickListener) {
        titleBar.setTitleBarRightFirstClick(onRightClickListener)
    }

    fun setTitleBarRightFirstVisibility(@Visibility visibility: Int) {
        titleBar.setRightFirstVisibility(visibility)
    }

    fun setTitleBarRightFirst(drawable: Int) {
        titleBar.setRightFirstDrawable(drawable)
    }
}
